﻿using Max.PH5_2008A.ShoppingCode.Domain;
using Max.PH5_2008A.ShoppingCode.IRespository;
using Max.PH5_2008A.ShoppingCode.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.PH5_2008A.ShoppingCode.Service.Product
{
    public class ProductCategoryService : BaseService<TbProductCategory>, IProductCategoryService
    {
        IProductCategoryRespository _respositry;
        public ProductCategoryService(IProductCategoryRespository respositry) : base(respositry)
        {
            _respositry = respositry;
        }

        public dynamic RecuresionCategory(string id)
        {
            var data = _respositry.Query().ToList();
            var result = this.GetRecuresionCategory(data,id);
            return result;
        }

        //递归分类
        private dynamic GetRecuresionCategory(List<TbProductCategory> data, string id)
        {
            var list = data.Where(x => id.Equals(Convert.ToString(x.Pid))).ToList();

            List<dynamic> result = new();
            list.ForEach(x => {
                var c = this.GetRecuresionCategory(data,x.Id.ToString());
                result.Add(new
                {
                    id = x.Id,
                    pid = x.Pid,
                    icon = x.Icon,
                    item = c==null ? null : c
                });
            });
            return result;
        }
    }
}
